package com.itheima.demo.service;

import com.itheima.demo.dto.AddDeptDto;
import com.itheima.demo.dto.UpdateDeptDtp;
import com.itheima.demo.entity.Dept;
import com.itheima.demo.mapper.DeptMapper;
import com.itheima.demo.vo.DeptV0;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServicelmpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<DeptV0> depts(){

        return deptMapper.selectAll();
    }

    @Override
    public void deleById(Integer id) {
        deptMapper.deleById(id);
    }

    @Override
    public void addDept(AddDeptDto dto) {
        Dept dept = new Dept();
        dept.setName(dto.name);
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public Dept getById(Integer id) {
       Dept dept= deptMapper.selectById(id);
        return dept;
    }

    @Override
    public void updateDept(UpdateDeptDtp dto) {
        Dept dept = new Dept();
        dept.setName(dto.getName());
        dept.setUpdateTime(LocalDateTime.now());
        dept.setId(dto.getId());
        deptMapper.update(dept);
    }
}
